/** \file integratedtests.cpp 
	Defines the entry point for the console application.
	This file also forms the start point for CeeFIT tests
*/

#include "stdafx.h"

#if !defined(SOLIDWORKS_HEADERS)
#define SOLIDWORKS_HEADERS
// Solidworks includes
#include <amapp.h>
#include <swpublished_i.h>
#include <swpublished_i.c>
#include <sldworks_i.c>
#include <swconst.h>
#endif

#include "SWX.h"

using namespace std;
using namespace CEEFIT;

/** The global pointer to the SolidWorks App. This has been made global as I don't know how to inject this value into CeeFIT tests
	\author GRI
	\date Oct 31 2008
*/
CComPtr<ISldWorks> swApp;

static void* ceefit_call_spec AllocForCeeFit(size_t numBytes)
{
  return(new unsigned char[numBytes]);
}


static void ceefit_call_spec FreeForCeeFit(void* objPtr)
{
  if(objPtr != NULL)
  {
    delete [] ((unsigned char*) objPtr);
  }
}


::CEEFITALLOCFUNC ceefit_call_spec GetCeeFitAllocFunc()
{
  return(AllocForCeeFit);
}


::CEEFITFREEFUNC ceefit_call_spec GetCeeFitFreeFunc()
{
  return(FreeForCeeFit);
}

/** This is the entry point and where CeeFIT gets called. If you build this program then an executable is created in the configuration folder named swxbatch_ceefit.exe. To run this program use the following command line:
	swxbatch_ceefit.exe -CeeFIT <input_file_name>.htm <output_file_name>.htm
*/
int __cdecl main(int argc, char** argv)
{
	using namespace swxbatch_ceefit;
	int retVal;
	RESULTS ceeResults;

	SWX oSwx;
	swApp = oSwx.StartSWX();
	// command line arguments to allow easier identification of input/output variables
	for( int i = 0; i < argc; i++ ) {
		wcout << "\nargument \t" << i << "\t=\t" << argv[i] << "\n";
	}
	// continue test
	if( swApp ) {
		if((retVal = CEEFIT::Run(argc, argv, ceeResults)) > 0) 
		{
			printf("\nCeeFIT:  %s\n", ceeResults.GetWrong() == 0 ? "All tests passed" : "Failed tests found");
			wcout << endl << ceeResults.GetCountsSummary() << endl;
			if( ceeResults.GetWrong() > 0 ) retVal = 1;
			else retVal = 0;
			oSwx.StopSWX(swApp);
			swApp.Release();
			CoUninitialize();
			return retVal;
		}
		else
		{
			// run the program's main loop when CeeFIT does nothing 
			cout << "CeeFIT did not run. Return value is: " << retVal << endl;
			oSwx.StopSWX(swApp);
			swApp.Release();
			CoUninitialize();
			return retVal;
		}
	}
}

